package com.zhss.eshop.schedule.stock;


import com.zhss.eshop.schedule.domain.model.ScheduleGoodsAllocationStock;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsAllocationStockDetail;
import com.zhss.eshop.schedule.domain.model.ScheduleGoodsStock;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsAllocationStockDetailMapper;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsAllocationStockMapper;
import com.zhss.eshop.schedule.mapper.ScheduleGoodsStockMapper;
import com.zhss.eshop.wms.domain.dto.WmsGoodsAllocationStockDetailDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderItemDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderPutOnItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 采购入库库存更新组件
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class PurchaseInputScheduleStockUpdater extends AbstractScheduleStockUpdater {

	/**
	 * 采购入库单
	 */
	private WmsPurchaseInputOrderDTO purchaseInputOrder;

	/**
	 * 商品库存管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsStockMapper goodsStockDAO;
	/**
	 * 货位库存管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsAllocationStockMapper goodsAllocationStockDAO;
	/**
	 * 货位库存明细管理的DAO组件
	 */
	@Autowired
	private ScheduleGoodsAllocationStockDetailMapper stockDetailDAO;

	/**
	 * 更新商品库存
	 */
	@Override
	protected void updateGoodsStock() throws Exception {
		List<WmsPurchaseInputOrderItemDTO> purchaseInputOrderItems =
				purchaseInputOrder.getItems();
		for(WmsPurchaseInputOrderItemDTO purchaseInputOrderItem : purchaseInputOrderItems) {
			ScheduleGoodsStock goodsStock = goodsStockDAO.getBySkuId(
					purchaseInputOrderItem.getGoodsSkuId());
			goodsStock.setAvailableStockQuantity(goodsStock.getAvailableStockQuantity()
					+ purchaseInputOrderItem.getArrivalCount());
			goodsStockDAO.updateByPrimaryKeySelective(goodsStock);
		}
	}

	/**
	 * 更新货位库存
	 */
	@Override
	protected void updateGoodsAllocationStock() throws Exception {
		List<WmsPurchaseInputOrderItemDTO> items = purchaseInputOrder.getItems();

		for(WmsPurchaseInputOrderItemDTO item : items) {
			List<WmsPurchaseInputOrderPutOnItemDTO> putOnItems = item.getPutOnItemDTOs();

			for(WmsPurchaseInputOrderPutOnItemDTO putOnItem : putOnItems) {
				ScheduleGoodsAllocationStock goodsAllocationStock = goodsAllocationStockDAO
						.getBySkuId(putOnItem.getGoodsAllocationId(), putOnItem.getGoodsSkuId());
				goodsAllocationStock.setAvailableStockQuantity(goodsAllocationStock.getAvailableStockQuantity()
						+ putOnItem.getPutOnShelvesCount());
				goodsAllocationStockDAO.updateByPrimaryKeySelective(goodsAllocationStock);
			}
		}
	}

	/**
	 * 更新货位库存明细
	 */
	@Override
	protected void updateGoodsAllocationStockDetail() throws Exception {
		List<WmsPurchaseInputOrderItemDTO> items = purchaseInputOrder.getItems();

		for(WmsPurchaseInputOrderItemDTO item : items) {
			List<WmsGoodsAllocationStockDetailDTO> stockDetails = item.getStockDetails();

			for(WmsGoodsAllocationStockDetailDTO stockDetail : stockDetails) {
				stockDetailDAO.insertSelective(stockDetail.clone(ScheduleGoodsAllocationStockDetail.class));
			}
		}
 	}

	/**
	 * 设置需要的参数
	 */
	@Override
	public void setParameter(Object parameter) {
		this.purchaseInputOrder = (WmsPurchaseInputOrderDTO) parameter;
	}

}
